//
// Created by 32827 on 2023/11/26.
//
#include "yz_i2c.h"
YZF_RESULT yz_i2c_add7_high_open(YZF_I2C_POINTER i2c,uint8_t address,uint32_t clockFreq){
    yzf_i2c_open(i2c);
    yzf_i2c_soft_reset(i2c);
    yzf_i2c_soft_reset_clear(i2c);

    yzf_i2c_set_high_mode(i2c);
    yzf_i2c_duty_cycle_2(i2c);
    yzf_i2c_source_clock_freq(i2c,clockFreq);
    yzf_i2c_set_CCR(i2c,(clockFreq*1000)/(3*400));//36*1000khz/400*3khz
    yzf_i2c_trise(i2c,(3*clockFreq/10)+1);
    yzf_i2c_set_address_7(i2c,address);

    yzf_i2c_ack_enable(i2c);
    yzf_i2c_enable(i2c);
    return YZF_OK;
}
YZF_RESULT yz_i2c_add7_normal_open(YZF_I2C_POINTER i2c,uint8_t address,uint32_t clockFreq){
    yzf_i2c_open(i2c);
    yzf_i2c_soft_reset(i2c);
    yzf_i2c_soft_reset_clear(i2c);

    yzf_i2c_set_normal_mode(i2c);
    yzf_i2c_duty_cycle_2(i2c);
    yzf_i2c_source_clock_freq(i2c,clockFreq);
    yzf_i2c_set_CCR(i2c,(clockFreq*1000)/(2*100));
    yzf_i2c_trise(i2c,(10*clockFreq/10)+1);
    yzf_i2c_set_address_7(i2c,address);

    yzf_i2c_ack_enable(i2c);
    yzf_i2c_enable(i2c);
    return YZF_OK;
}
YZF_RESULT yz_i2c_add7_transmit(YZF_I2C_POINTER i2c,uint8_t address,uint8_t* data,uint16_t length){
    yzf_i2c_ack_enable(i2c);
    while (yzf_i2c_flag_BUSY(i2c)==YZF_TRUE);

    // yzf_i2c_start(i2c);
    // while (yzf_i2c_flag_SB(i2c)==YZF_FALSE);

    // yzf_i2c_write_data(i2c,(address<<1)+YZF_I2C_Write);
	YZF_REGV32 HR_I2C_TX_RX YZF_SET (address<<1)+0;
	YZF_REGV32 HR_I2C_CR_SR YZF_SET I2C_CR_WR|I2C_CR_STA;

    while (yzf_i2c_flag_ADDR(i2c)==YZF_FALSE);
    uint8_t j=yzf_i2c_flag_TRA(i2c);//���ADDR

    for(int i=0;i<length;i++){
        while (yzf_i2c_flag_TxE(i2c)==YZF_FALSE);
        yzf_i2c_write_data(i2c,data[i]);
    }
    while (yzf_i2c_flag_TxE(i2c)==YZF_FALSE);
    yzf_i2c_stop(i2c);

    yzf_i2c_ack_disable(i2c);
    return YZF_OK;
}

YZF_RESULT yz_i2c_add7_receive(YZF_I2C_POINTER i2c,uint8_t address,uint8_t* data,uint16_t length){
    yzf_i2c_ack_enable(i2c);
    while (yzf_i2c_flag_BUSY(i2c)==YZF_TRUE);

    yzf_i2c_start(i2c);
    while (yzf_i2c_flag_SB(i2c)==YZF_FALSE);

    yzf_i2c_write_data(i2c,(address<<1)+YZF_I2C_Read);
    while (yzf_i2c_flag_ADDR(i2c)==YZF_FALSE);
    uint8_t j=yzf_i2c_flag_TRA(i2c);//���ADDR

    if(length==1){
        yzf_i2c_ack_disable(i2c);
        yzf_i2c_stop(i2c);

        while(yzf_i2c_flag_RxNE(i2c)==YZF_FALSE);
        *(data)=yzf_i2c_read_data(i2c);
        return YZF_OK;
    }

    for(int i=0;i<length;i++){
        while(yzf_i2c_flag_RxNE(i2c)==YZF_FALSE);
        *(data+i)=yzf_i2c_read_data(i2c);
        if(i+2==length){
            yzf_i2c_ack_disable(i2c);
            yzf_i2c_stop(i2c);
        }
    }
    return YZF_OK;
}
YZF_RESULT yz_i2c_add7_mem_receive(YZF_I2C_POINTER i2c,uint8_t address,uint8_t memAddress,uint8_t* data,uint16_t length){

    yzf_i2c_ack_enable(i2c);
    while (yzf_i2c_flag_BUSY(i2c)==YZF_TRUE);

    yzf_i2c_start(i2c);
    while (yzf_i2c_flag_SB(i2c)==YZF_FALSE);

    yzf_i2c_write_data(i2c,(address<<1)+YZF_I2C_Write);
    while (yzf_i2c_flag_ADDR(i2c)==YZF_FALSE);
    uint8_t j=yzf_i2c_flag_TRA(i2c);//���ADDR

    yzf_i2c_write_data(i2c,memAddress);
    while (yzf_i2c_flag_TxE(i2c)==YZF_FALSE);

    yzf_i2c_start(i2c);
    while (yzf_i2c_flag_SB(i2c)==YZF_FALSE);

    yzf_i2c_write_data(i2c,(address<<1)+YZF_I2C_Read);
    while (yzf_i2c_flag_ADDR(i2c)==YZF_FALSE);
    j=yzf_i2c_flag_TRA(i2c);//���ADDR

    if(length==1){
        yzf_i2c_ack_disable(i2c);
        yzf_i2c_stop(i2c);

        while(yzf_i2c_flag_RxNE(i2c)==YZF_FALSE);
        *(data)=yzf_i2c_read_data(i2c);
        return YZF_OK;
    }

    for(int i=0;i<length;i++){
        while(yzf_i2c_flag_RxNE(i2c)==YZF_FALSE);
        *(data+i)=yzf_i2c_read_data(i2c);
        if(i+2==length){
            yzf_i2c_ack_disable(i2c);
            yzf_i2c_stop(i2c);
        }
    }
    return YZF_OK;
}
YZF_RESULT yz_i2c_add7_mem16_receive(YZF_I2C_POINTER i2c,uint8_t address,uint16_t memAddress,uint8_t* data,uint16_t length){

    yzf_i2c_ack_enable(i2c);
    while (yzf_i2c_flag_BUSY(i2c)==YZF_TRUE);

    yzf_i2c_start(i2c);
    while (yzf_i2c_flag_SB(i2c)==YZF_FALSE);

    yzf_i2c_write_data(i2c,(address<<1)+YZF_I2C_Write);
    while (yzf_i2c_flag_ADDR(i2c)==YZF_FALSE);
    uint8_t j=yzf_i2c_flag_TRA(i2c);//���ADDR

    yzf_i2c_write_data(i2c,*(uint8_t *)&memAddress);//Address [0:7]
    while (yzf_i2c_flag_TxE(i2c)==YZF_FALSE);

    yzf_i2c_write_data(i2c,*(((uint8_t *)&memAddress)+1));//Address [8:15]
    while (yzf_i2c_flag_TxE(i2c)==YZF_FALSE);

    yzf_i2c_start(i2c);
    while (yzf_i2c_flag_SB(i2c)==YZF_FALSE);

    yzf_i2c_write_data(i2c,(address<<1)+YZF_I2C_Read);
    while (yzf_i2c_flag_ADDR(i2c)==YZF_FALSE);
    j=yzf_i2c_flag_TRA(i2c);//���ADDR

    if(length==1){
        yzf_i2c_ack_disable(i2c);
        yzf_i2c_stop(i2c);

        while(yzf_i2c_flag_RxNE(i2c)==YZF_FALSE);
        *(data)=yzf_i2c_read_data(i2c);
        return YZF_OK;
    }

    for(int i=0;i<length;i++){
        while(yzf_i2c_flag_RxNE(i2c)==YZF_FALSE);
        *(data+i)=yzf_i2c_read_data(i2c);
        if(i+2==length){
            yzf_i2c_ack_disable(i2c);
            yzf_i2c_stop(i2c);
        }
    }
    return YZF_OK;
}
YZF_RESULT yz_i2c_add7_mem_transmit(YZF_I2C_POINTER i2c,uint8_t address,uint8_t memAddress,uint8_t* data,uint16_t length){
    yzf_i2c_ack_enable(i2c);
    while (yzf_i2c_flag_BUSY(i2c)==YZF_TRUE);

    yzf_i2c_start(i2c);
    while (yzf_i2c_flag_SB(i2c)==YZF_FALSE);

    yzf_i2c_write_data(i2c,(address<<1)+YZF_I2C_Write);
    while (yzf_i2c_flag_ADDR(i2c)==YZF_FALSE);
    uint8_t j=yzf_i2c_flag_TRA(i2c);//���ADDR

    yzf_i2c_write_data(i2c,memAddress);
    while (yzf_i2c_flag_TxE(i2c)==YZF_FALSE);

    for(int i=0;i<length;i++){
        while(yzf_i2c_flag_TxE(i2c)==YZF_FALSE);
        yzf_i2c_write_data(i2c,data[i]);
    }

    while (yzf_i2c_flag_BTF(i2c)==YZF_FALSE);
    while(yzf_i2c_flag_TxE(i2c)==YZF_FALSE);
    yzf_i2c_stop(i2c);

    yzf_i2c_ack_disable(i2c);
    return YZF_OK;
}
YZF_RESULT yz_i2c_add7_mem16_transmit(YZF_I2C_POINTER i2c,uint8_t address,uint16_t memAddress,uint8_t* data,uint16_t length){
    yzf_i2c_ack_enable(i2c);
    while (yzf_i2c_flag_BUSY(i2c)==YZF_TRUE);

    yzf_i2c_start(i2c);
    while (yzf_i2c_flag_SB(i2c)==YZF_FALSE);

    yzf_i2c_write_data(i2c,(address<<1)+YZF_I2C_Write);
    while (yzf_i2c_flag_ADDR(i2c)==YZF_FALSE);
    uint8_t j=yzf_i2c_flag_TRA(i2c);//���ADDR

    yzf_i2c_write_data(i2c,*(uint8_t *)&memAddress);
    while (yzf_i2c_flag_TxE(i2c)==YZF_FALSE);

    yzf_i2c_write_data(i2c,*(((uint8_t *)&memAddress)+1));
    while (yzf_i2c_flag_TxE(i2c)==YZF_FALSE);

    for(int i=0;i<length;i++){
        while(yzf_i2c_flag_TxE(i2c)==YZF_FALSE);
        yzf_i2c_write_data(i2c,data[i]);
    }

    while (yzf_i2c_flag_BTF(i2c)==YZF_FALSE);
    while(yzf_i2c_flag_TxE(i2c)==YZF_FALSE);
    yzf_i2c_stop(i2c);

    yzf_i2c_ack_disable(i2c);
    return YZF_OK;
}
